package ppd;

import java.util.HashMap;
import java.util.Map;

/**
 * 无重复最长子串
 */
class Solution {
    public static int lengthOfLongestSubstring(String s) {
        if(s.isEmpty()){
            return 0;
        }
        Map<Character, Integer> charIndexMap = new HashMap<>(s.length() * 2);
        int res = 0;
        int right = 0;
        int left = 0;
//        "tmmzuxt"
        for (char element : s.toCharArray()) {
            Integer elIndex = charIndexMap.get(element);
            if(elIndex != null && elIndex > left){
                res = Math.max(res, right - left);
                charIndexMap.put(element, right);
                left = elIndex + 1;
            }else{
                charIndexMap.put(element, right);
                if(right == s.length() - 1){
                    res = Math.max(res, right - left + 1);
                    break;
                }
            }
            right++;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("tmmzuxt"));
    }
}